//
//  DetailViewController.m
//  MarryMe
//
//  Created by lanou3g on 16/1/6.
//  Copyright © 2016年 revocher. All rights reserved.
//

#import "DetailViewController.h"
#import "ClassDataTool.h"
#import "UIImageView+WebCache.h"
#import "MyStoreCollectionViewCell.h"
#import "CasesTableViewCell.h"
#import "InfomationTableViewCell.h"
#import "CaseViewController.h"
#import "AllCaseViewController.h"
@interface DetailViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (strong, nonatomic) IBOutlet UITableView *mainTabelView;


@property (strong, nonatomic)NSString *backImage;
@property (strong, nonatomic)NSString *logoImage;
@property (strong, nonatomic)NSString *name;

@property (strong, nonatomic)NSString *zhiye;
@property (strong, nonatomic)NSString *address;
@property (strong, nonatomic)NSString *Email;
@property (strong, nonatomic)NSString *phone;

@property (strong, nonatomic)NSString *des;

@property(nonatomic,strong)NSMutableArray *idArray;
@property(nonatomic,strong)NSMutableArray *imageArray;
@property(nonatomic,strong)NSMutableArray *titleArray;
@property(nonatomic,strong)NSString *works_count;

@property(nonatomic,strong)NSString *leftImage;
@property(nonatomic,strong)NSString *rightImage;
@property(nonatomic,strong)NSString *leftTitle;
@property(nonatomic,strong)NSString *rightTitle;

@property(nonatomic,strong)NSString *secondImage;



@end

@implementation DetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.idArray = [NSMutableArray array];
    self.imageArray = [NSMutableArray array];
    self.titleArray = [NSMutableArray array];

    NSString *str = [@"%E5%AE%9A%E5%88%B6%E5%A9%9A%E7%BA%B1" stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"utf8=%@",[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]);
    NSLog(@"str =%@",str);
    self.mainTabelView.dataSource = self;
    self.mainTabelView.delegate = self;
    
    //cell如果是xib文件,使用以下方式去注册
    [self.mainTabelView registerNib:[UINib nibWithNibName:@"CasesTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"CasesTableViewCell"];
    [self.mainTabelView registerNib:[UINib nibWithNibName:@"InfomationTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"InfomationTableViewCell"];
    [self.mainTabelView reloadData];
   
    
    ///接受来自CasesTableViewCell的通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(leftAction:) name:@"leftImage" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(rightAction:) name:@"rightImage" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(allAction:) name:@"allCaseImage" object:nil];
}

-(void)leftAction:(NSNotification *)sender{
    CaseViewController *caseVC = [[CaseViewController alloc]init];
    caseVC.ids = self.idArray[0];
    [self.navigationController pushViewController:caseVC animated:YES];
     //self.navigationController.navigationBar.hidden = YES;
}

-(void)rightAction:(NSNotification *)sender{
    CaseViewController *caseVC = [[CaseViewController alloc]init];
    caseVC.ids = self.idArray[1];
    [self.navigationController pushViewController:caseVC animated:YES];
  //  self.navigationController.navigationBar.hidden = YES;
}

-(void)allAction:(NSNotification *)sender{
    AllCaseViewController *allCaseVC = [[AllCaseViewController alloc]init];
    allCaseVC.ids = self.ids;
    [self.navigationController pushViewController:allCaseVC animated:YES];
     //self.navigationController.navigationBar.hidden = YES;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        InfomationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"InfomationTableViewCell"];
        [ClassDataTool requestDataWithUrl:[NSString stringWithFormat:@"http://www.hunliji.com/p/wedding/index.php/home/APIMerchant/detailMerchant/?mer_id=%@",self.ids] WithBackBlock:^(id object) {
            NSDictionary *resultDic = (NSDictionary *)object;
            NSDictionary *dataDic = resultDic[@"data"];
            NSDictionary *merchantDic = dataDic[@"merchant"];
            self.logoImage =merchantDic[@"logo_path"];
            self.backImage =merchantDic[@"cover_path"];
            self.address = merchantDic[@"address"];
            self.name = merchantDic[@"name"];
            self.Email =  merchantDic[@"email"];
            self.des =  merchantDic[@"des"];
            self.phone =  [merchantDic[@"phone"] firstObject];
            self.secondImage = merchantDic[@"back_img"];
            //判断是否为空
            if (![self.backImage isKindOfClass:[NSNull class]]) {
                [cell.backImage sd_setImageWithURL:[NSURL URLWithString:self.backImage]];
            }else{
                [cell.backImage sd_setImageWithURL:[NSURL URLWithString:merchantDic[@"back_img"]]];
            }
            if (![self.logoImage isKindOfClass:[NSNull class]]) {
                [cell.logoImage sd_setImageWithURL:[NSURL URLWithString:self.logoImage]];

            }

            cell.logoImage.layer.cornerRadius = 40;
            cell.logoImage.clipsToBounds = YES;
            cell.nameLabel.text = self.name;
            cell.zhiyeLabel.text = self.zhiye;
            cell.addressLabel.text = self.address;
            cell.emailLabel.text = self.Email;
            cell.desLabel.text = self.des;
            cell.phoneLabel.text = self.phone;
        }];

        return  cell;
    }
    if (indexPath.section == 1) {
        CasesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CasesTableViewCell"];
        [ClassDataTool requestDataWithUrl:[NSString stringWithFormat:@"http://www.hunliji.com/p/wedding/index.php/home/APIMerchant/GetMerchantMeals/id/%@?per_page=2&kind=case",self.ids] WithBackBlock:^(id object) {
            NSDictionary *resultDic = (NSDictionary *)object;
            NSArray *worksArray = resultDic[@"works"];
            self.works_count = resultDic[@"works_count"];
            if ([self.works_count isEqualToString:@"0"]) {
                cell.leftImageView.image = nil;
                cell.rightImageView.image = nil;
                cell.allCaseImageView.hidden = YES;
                cell.allCaseImageView.userInteractionEnabled = NO;
                cell.leftWiteImage.userInteractionEnabled = NO;
                cell.rightWhiteImage.userInteractionEnabled = NO;
                cell.anliLabel.text = @"";
            }else{
                for (NSDictionary *dataDic in worksArray) {
                    NSString *ids = dataDic[@"id"];
                    NSString *cover_path = dataDic[@"cover_path"];
                    NSString *title = dataDic[@"title"];
                    [self.idArray addObject:ids];
                    [self.imageArray addObject:cover_path];
                    [self.titleArray addObject:title];
                    //NSLog(@"%@",self.works_count);
                    //NSLog(@"--------%@",ids);
                }
                if ([self.works_count isEqualToString:@"1"]) {
                    self.leftImage = self.imageArray[0];
                    self.leftTitle = self.titleArray[0];
                    [cell.leftImageView sd_setImageWithURL:[NSURL URLWithString:self.leftImage]];
                    cell.leftTitle.text = self.leftTitle;
                    cell.leftWiteImage.layer.borderWidth = 0.3;
                    cell.leftWiteImage.layer.borderColor = [[UIColor grayColor]CGColor];
                    
                    cell.rightImageView.image = nil;
                    cell.rightWhiteImage.userInteractionEnabled = NO;
                    cell.rightTitle.text = @"";
                    cell.allCaseImageView.hidden = YES;
                    cell.allCaseImageView.userInteractionEnabled = NO;
                    
                }else{
                    self.leftImage = self.imageArray[0];
                    self.rightImage = self.imageArray[1];
                    self.leftTitle = self.titleArray[0];
                    self.rightTitle = self.titleArray[1];
                    
                    [cell.leftImageView sd_setImageWithURL:[NSURL URLWithString:self.leftImage]];
                    [cell.rightImageView sd_setImageWithURL:[NSURL URLWithString:self.rightImage]];
                    cell.leftTitle.text = self.leftTitle;
                    cell.rightTitle.text = self.rightTitle;
                    cell.leftWiteImage.layer.borderWidth = 0.3;
                    cell.leftWiteImage.layer.borderColor = [[UIColor grayColor]CGColor];
                    cell.rightWhiteImage.layer.borderWidth = 0.3;
                    cell.rightWhiteImage.layer.borderColor = [[UIColor grayColor]CGColor];
                    
                    cell.backImage.layer.borderWidth = 0.5;
                    cell.backImage.layer.borderColor = [[UIColor purpleColor]CGColor];
                }
            }
           
        }];

        
    
        return cell;
    }
    return nil;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 650;
    }if (indexPath.section == 1) {
        return 230;
    }
    return 0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 20;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark  --- 移除所有通知的监听
-(void)dealloc{
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    //移除所有通知的监听
    [center removeObserver:self];
}

/*
-(void)getData{
    [DataTool requestDataWithUrl:[NSString stringWithFormat:@"http://www.hunliji.com/p/wedding/index.php/home/APIMerchant/detailMerchant/?mer_id=%@",self.ids] WithBackBlock:^(id object) {
        NSDictionary *resultDic = (NSDictionary *)object;
        NSDictionary *dataDic = resultDic[@"data"];
        NSDictionary *merchantDic = dataDic[@"merchant"];
        self.logoImage =merchantDic[@"logo_path"];
        self.backImage =merchantDic[@"cover_path"];
        self.address = merchantDic[@"address"];
        self.name = merchantDic[@"name"];
        self.Email =  merchantDic[@"email"];
        self.des =  merchantDic[@"des"];
        self.phone =  merchantDic[@"phone_400"];
        NSLog(@"%@",self.backImage);
        NSLog(@"%@",self.des);
    }];
}

-(void)getNetWork{
    [DataTool requestDataWithUrl:[NSString stringWithFormat:@"http://www.hunliji.com/p/wedding/index.php/home/APIMerchant/GetMerchantMeals/id/%@?per_page=2&kind=case",self.ids] WithBackBlock:^(id object) {
        NSDictionary *resultDic = (NSDictionary *)object;
        NSArray *worksArray = resultDic[@"works"];
        self.works_count = resultDic[@"works_count"];
       
        for (NSDictionary *dataDic in worksArray) {
            NSString *ids = dataDic[@"id"];
            NSString *cover_path = dataDic[@"cover_path"];
            NSString *title = dataDic[@"title"];
            [self.idArray addObject:ids];
            [self.imageArray addObject:cover_path];
            [self.titleArray addObject:title];
            NSLog(@"%@",self.works_count);
           
        }
//         NSLog(@"%@",self.idArray);
//        NSLog(@"%@",self.imageArray );
        self.leftImage = self.imageArray[0];
        self.rightImage = self.imageArray[1];
        self.leftTitle = self.titleArray[0];
        self.rightTitle = self.titleArray[1];


    }];
}
*/








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
